<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model;

class Login extends Controller
{
	public function index()
	{
		return $this->fetch('login');
	}

	//登陆效验
	public function check()
	{
		$username = $_POST['username'];
		$password = $_POST['password'];

		if(!trim($username)){
			return msg(0, '用户名不能为空');
		}
		if(!trim($password)){
			return msg(0, '密码不能为空');
		}

		$loginModel =  new model\Login();
		$res = $loginModel->getAdminByUsername($username);

		if(!$res){
			return msg(0, '用户名不存在');
		}
		if(!(md5($password) == $res[0]['password'])){
			return msg(0, '密码不正确');
		}
		//用户信息存储
		session('adminUser', $username);
		return msg(1, '登陆成功');
	}

	//退出登录
	public function logout()
	{
		//清除用户信息
		session('adminUser', null);
		$this->redirect('/admin/login/index');
	}

}
